<template>
    <div class="content">
        <el-row :gutter="20">
            <el-col :span="12">
                <!-- <el-button type="primary" :icon="CirclePlus" @click="onInsertType(-1)">添加分类</el-button> -->
                <el-button type="primary" :icon="CirclePlus" @click="onEdit(0)">添加</el-button>
            </el-col>
            <el-col :span="12" class="text-right">
                <el-input v-model="form.search" style="width: 240px" placeholder="请输入关键词" />
                <el-button type="primary" class="ml10" :icon="Search" @click="onSearch">搜索</el-button>
                <el-button type="primary" class="ml10" :icon="RefreshRight" @click="onSearch('Refresh')" />
            </el-col>
        </el-row>
        
            <el-table :data="tableData" border v-loading="form.loading" style="width: 100%;font-size: 16px;" @cell-dblclick="onCellDb">
                <el-table-column type="index" :index="indexMethod" label="序号" width="60" align="center" />
                <el-table-column label="所属项目" show-overflow-tooltip align="center">
                    <template #default="scope">
                        <el-text>{{scope.row.project.title}}</el-text>
                    </template>
                </el-table-column>
                <el-table-column prop="content" label="名称" show-overflow-tooltip align="center" />
                <el-table-column prop="recipient" label="交付方" width="110" align="center" />
                <el-table-column prop="remark" label="备注" width="80" align="center" />
                <el-table-column prop="delivery_date" label="交付日期" width="180" align="center" />
                <el-table-column fixed="right" label="操作" width="150" align="center">
                    <template #default="scope">
                        <el-link link type="primary" @click="onEdit(scope.row.id)" >编辑</el-link>
                        <el-popconfirm title="确定要删除么?" @confirm="onDelete(scope.row.id)">
                            <template #reference>
                                <el-link link type="danger" >删除</el-link>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
        <div class="mt20">
            <el-pagination v-model:current-page="form.current" v-model:page-size="form.limit" :page-sizes="[10, 20, 30, 40, 50, 100]" layout="total, sizes, prev, pager, next, jumper" :total="form.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
    </div>

    <el-dialog v-model="isType" title="请输入名称" width="30%">
        <el-input v-model="typeForm.title" />
        <el-divider />
        <div class="text-center">
            <el-button type="primary" @click="onSaveType" :loading="typeForm.loading"> 保存 </el-button>
        </div>
        
    </el-dialog>

    <el-dialog v-model="isEdit" title="" width="30%">
        <component :is="editForm" :data = editId @onSave="onSave" />
    </el-dialog>
</template>

<script lang="ts" setup>
import { ref,onMounted } from 'vue'
import editForm from './components/editForm.vue';
import { CirclePlus, RefreshRight, Search } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import http from '../../../util/request';
const indexMethod = (index: number) => {
  return index+= 1
}
const form = ref<any>({search:'',page:0,type_id:0,current:0,total:10,limit:20,loading:false,parentBorder:false})
const typeForm = ref<any>({title:'',id:0,loading:false})
const isEdit = ref<boolean>(false)
const editId = ref<number>(0)
const isType = ref<boolean>(false)
interface Types {
    id: number
    title: string
    created_at:string
}


interface Project {
    id: number
    title: string
    created_at: string
    project_id: number
    type_id: number
    content: string
    files: string
    recipient: string
    delivery_date: string
    remark: string
}

const tableData = ref<Project[]>([])
const typeData = ref<Types[]>([])

const onGetData = () => {
    form.value.loading = true
    http.post('/completion',form.value).then((response:any)=>{
        form.value.loading = false
        tableData.value = response.data.data
        typeData.value = response.data.type
        form.value.total = response.data.total
    })
}

const onSearch = (type = '') => {
    if(type == 'Refresh'){
        form.value.search = ''
        onGetData()
        return
    }
    if(!form.value.search){
        ElMessage.error('请输入搜索内容')
    }
    form.value.page = 1
    onGetData()
}


const onCellDb = (data: Project) => {
    console.log(data)
}

const onEdit = (index: number) => {
    editId.value = index
    isEdit.value = true
}

const onDelete = (id: number) => {
    http.delete('/completion/'+id).then((response:any)=>{
        if(response.data.code != 200){
            ElMessage.error(response.data.msg);return;
        }
        ElMessage.success(response.data.msg);
        onGetData();
    })
}


// const onSeletType = (id: number) => {
//     form.value.type_id = id
//     form.value.pege = 0
//     onGetData();
// }

// const onInsertType = (index: number) => {
//     typeForm.value.id = 0
//     typeForm.value.title = ''
//     if(index >= 0){
//         typeForm.value.id = typeData.value[index].id
//         typeForm.value.title = typeData.value[index].title
//     }
//     isType.value = true
// }
const onSaveType = () => {
    typeForm.value.loading = true
    let request:any = null
    if(typeForm.value.id){
        request = http.patch('/completion/type',typeForm.value)
    }else{
        request = http.put('/completion/type',typeForm.value)
    }
    request.then((response:any)=>{
        typeForm.value.loading = false
        if(response.data.code == 200){
            onGetData()
            isType.value = false
        }
    })
}

const handleSizeChange = (val: number) => {
    form.value.limit = val
    onGetData()
}
const handleCurrentChange = (val: number) => {
    form.value.page = val
    onGetData()
}
onMounted(()=>{
    onGetData()
})

const onSave = () => {
    isEdit.value = false
    onGetData()
}
</script>

<style scoped>

</style>